package com.zhenhao.xsq.ui.activity.login.presenter;

import com.blankj.utilcode.util.AppUtils;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.orhanobut.logger.Logger;
import com.zhenhao.xsq.contain.Contains;
import com.zhenhao.xsq.data.api.HttpAPIWrapper;
import com.zhenhao.xsq.ui.activity.login.SplashActivity;
import com.zhenhao.xsq.ui.activity.login.contract.SplashContract;
import com.zhenhao.xsq.utils.SpUtil;
import com.zhenhao.xsq.utils.ToastUtil;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

import static com.zhenhao.xsq.contain.Contains.toType;

/**
 * @author xlei
 * @Package com.example.rxdemo.ui.activity.login
 * @Description: presenter of SplashActivity
 * @date 2019/01/07 10:39:32
 */
public class SplashPresenter implements SplashContract.SplashContractPresenter {

    private final SplashContract.View mView;
    HttpAPIWrapper httpAPIWrapper;
    private CompositeDisposable mCompositeDisposable;
    private SplashActivity mActivity;



    @Inject
    public SplashPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull SplashContract.View
            view, SplashActivity activity) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mActivity = activity;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.dispose();
        }
    }

    @Override
    public void getPermission(final String[] permission) {

        XXPermissions.with(mActivity)
                // 申请单个权限
//                .permission(Permission.RECORD_AUDIO)
                // 申请多个权限
//                .permission(Permission.Group.CALENDAR)
                .permission(permission)
                // 设置权限请求拦截器（局部设置）
//                .interceptor(new PermissionInterceptor())
                // 设置不触发错误检测机制（局部设置）
                //.unchecked()
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            Logger.i("权限申请成功 " + permissions);
                        } else {
//                            toast("获取部分权限成功，但部分权限未正常授予");
                            Logger.i("获取部分权限成功，但部分权限未正常授予 " + permissions);
                            ToastUtil.showLong("部分权限获取失败");
//                            finish();
                        }

                        mActivity.agreePrvite();
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
//                            toast("被永久拒绝授权，请手动授予录音和日历权限");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                            XXPermissions.startPermissionActivity(context, permissions);
                            Logger.i("被永久拒绝授权，请手动授予 " + permissions);
                            ToastUtil.showLong("被永久拒绝授权，请手动授予");
//                            finish();
                        } else {
//                            toast("获取录音和日历权限失败");
                            Logger.i("获取权限失败 " + permissions);
                            ToastUtil.showLong("获取权限失败");
//                            finish();
                        }
                        mActivity.agreePrvite();
                    }
                });
    }

    public void skipActivity(int type) {
        if (AppUtils.getAppVersionCode() > SpUtil.getInt(this.mActivity, Contains.SP_APP_VERSION, 0)) {
            toType = 0;
            SpUtil.putInt(this.mActivity, Contains.SP_APP_ISFIRST, 0);
        }

        Observable.interval(0, 1, TimeUnit.SECONDS).take(2)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(@NonNull Long aLong) {
                        return 2 - aLong;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//发射用的是observeOn
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) {
                        Logger.i("1");
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Logger.i("2");
                    }

                    @Override
                    public void onNext(@NonNull Long remainTime) {
                        Logger.i("剩余时间" + remainTime);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Logger.i("4");
                    }

                    @Override
                    public void onComplete() {
//
                        if (toType == 0) {
//                            mView.toGuide();
                            mView.toLogin();
                        } else if (toType == 1) {
                            mView.toLogin();
                        } else if (toType == 2) {
                            mView.toHome();
                        }
                    }
                });

    }
}